package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.CustomDeptUnifiedPermission;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDepartPermission;
import org.jeecg.modules.system.mapper.CustomDeptUnifiedPermissionMapper;
import org.jeecg.modules.system.service.ICustomDeptUnifiedPermissionService;
import org.jeecg.modules.system.service.ISysDepartPermissionService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 部门统一权限表
 * @Author: jeecg-boot
 * @Date: 2021-12-18
 * @Version: V1.0
 */
@Service
public class CustomDeptUnifiedPermissionServiceImpl extends ServiceImpl<CustomDeptUnifiedPermissionMapper, CustomDeptUnifiedPermission> implements ICustomDeptUnifiedPermissionService {

    @Autowired
    ISysDepartService departService;
    @Autowired
    ISysDepartPermissionService departPermissionService;

    @Override
    public void savePermission(String permissionIds, String lastPermissionIds) {
        List<SysDepart> departList = departService.list();
        List<String> add = getDiff(lastPermissionIds, permissionIds);
        if (add != null && add.size() > 0) {
            List<CustomDeptUnifiedPermission> list = new ArrayList<CustomDeptUnifiedPermission>();
            for (String p : add) {
                if (oConvertUtils.isNotEmpty(p)) {
                    CustomDeptUnifiedPermission deptUnifiedPermission = new CustomDeptUnifiedPermission(p);
                    list.add(deptUnifiedPermission);
                }
            }
            boolean flag = this.saveBatch(list);
            // 查找所有部门 关联权限
            if (flag) {
                for (SysDepart depart : departList) {
                    for (String s : add) {
                        departPermissionService.save(new SysDepartPermission(depart.getId(), s));
                    }
                }
            }
        }

        List<String> delete = getDiff(permissionIds, lastPermissionIds);
        if (delete != null && delete.size() > 0) {
            for (String permissionId : delete) {
                boolean flag = this.remove(new QueryWrapper<CustomDeptUnifiedPermission>().lambda().eq(CustomDeptUnifiedPermission::getPermissionId, permissionId));
                // 查找所有部门 删除权限
                if (flag) {
                    for (SysDepart depart : departList) {
                        departPermissionService.remove(Wrappers.lambdaQuery(SysDepartPermission.class).eq(SysDepartPermission::getDepartId,depart.getId()).in(SysDepartPermission::getPermissionId,delete));
                    }
                }
            }
        }
    }

    /**
     * 从diff中找出main中没有的元素
     *
     * @param main
     * @param diff
     * @return
     */
    private List<String> getDiff(String main, String diff) {
        if (oConvertUtils.isEmpty(diff)) {
            return null;
        }
        if (oConvertUtils.isEmpty(main)) {
            return Arrays.asList(diff.split(","));
        }

        String[] mainArr = main.split(",");
        String[] diffArr = diff.split(",");
        Map<String, Integer> map = new HashMap<>();
        for (String string : mainArr) {
            map.put(string, 1);
        }
        List<String> res = new ArrayList<String>();
        for (String key : diffArr) {
            if (oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
                res.add(key);
            }
        }
        return res;
    }
}
